import { FC, useEffect, lazy } from 'react'
import { RouteComponentProps } from 'react-router'
import { Layout, Spin } from 'antd'
import useUserInfo from '@/hooks/user'
import { useStore } from '@/store'
import { observer } from 'mobx-react'
import AppSider from './Sider'

import './index.scss'

const AppHeader = lazy(() => import('./Header'))
const AppContent = lazy(() => import('./Content'))

const AppLayout: FC<RouteComponentProps> = () => {
  // 登录后me接口信息存入mobx-userStroe
  const { userData, loading } = useUserInfo()
  const { userStore } = useStore()
  const setUserStore = () => {
    userStore.getUserData(userData)
  }
  useEffect(() => {
    setUserStore()
    // eslint-disable-next-line
  }, [userData])

  const appBody = () => {
    if (loading) return <Spin />
    return (
      <Layout style={{ height: '100vh', overflow: 'auto' }}>
        <div className="sider-box">
          <AppSider />
        </div>
        <Layout style={{ height: '100vh', overflow: 'auto' }}>
          <AppHeader />
          <AppContent />
        </Layout>
      </Layout>
    )
  }
  return appBody()
}

export default observer(AppLayout)
